#include "cameraPanel.h"

#define MAX 5000

CameraPanel::CameraPanel(QWidget* _parent, Camera *_c) : QWidget(_parent), camera(_c){
    Vector3D eye = camera->getEye();
    Vector3D lookAt = camera->getLookAt();
    Vector3D up = camera->getUp();
    eyeX = eye.x;
    eyeY = eye.y;
    eyeZ = eye.z;
    lookAtX = lookAt.x;
    lookAtY = lookAt.y;
    lookAtZ = lookAt.z;
    upX = up.x;
    upY = up.y;
    upZ = up.z;
    m = camera->getM();
    n = camera->getN();
    f = camera->getFocalDistance();




    QLabel *eyeXLabel = new QLabel(tr("x:"));

    QDoubleSpinBox *eyeXDoubleSpinBox = new QDoubleSpinBox();
    eyeXDoubleSpinBox->setRange(-50.0, 50.0);
    eyeXDoubleSpinBox->setSingleStep(1.0);
    eyeXDoubleSpinBox->setValue(eyeX);
    connect(eyeXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeEyeX(double)));


    QLabel *eyeYLabel = new QLabel(tr("y:"));

    QDoubleSpinBox *eyeYDoubleSpinBox = new QDoubleSpinBox();
    eyeYDoubleSpinBox->setRange(-50.0, 50.0);
    eyeYDoubleSpinBox->setSingleStep(1.0);
    eyeYDoubleSpinBox->setValue(eyeY);
    connect(eyeYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeEyeY(double)));


    QLabel *eyeZLabel = new QLabel(tr("z:"));

    QDoubleSpinBox *eyeZDoubleSpinBox = new QDoubleSpinBox();
    eyeZDoubleSpinBox->setRange(-50.0, 50.0);
    eyeZDoubleSpinBox->setSingleStep(1.0);
    eyeZDoubleSpinBox->setValue(eyeZ);
    connect(eyeZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeEyeZ(double)));


    QHBoxLayout *eyeLayout = new QHBoxLayout;
    eyeLayout->addWidget(eyeXLabel);
    eyeLayout->addWidget(eyeXDoubleSpinBox);
    eyeLayout->addStretch(1);
    eyeLayout->addWidget(eyeYLabel);
    eyeLayout->addWidget(eyeYDoubleSpinBox);
    eyeLayout->addStretch(1);
    eyeLayout->addWidget(eyeZLabel);
    eyeLayout->addWidget(eyeZDoubleSpinBox);
    eyeLayout->addStretch(1);

    QGroupBox *eyeGroupBox = new QGroupBox(QString::fromUtf8("Eye (ponto)"));
    eyeGroupBox->setLayout(eyeLayout);




    QLabel *lookAtXLabel = new QLabel(tr("x:"));

    QDoubleSpinBox *lookAtXDoubleSpinBox = new QDoubleSpinBox();
    lookAtXDoubleSpinBox->setRange(-50.0, 50.0);
    lookAtXDoubleSpinBox->setSingleStep(1.0);
    lookAtXDoubleSpinBox->setValue(lookAtX);
    connect(lookAtXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeLookAtX(double)));


    QLabel *lookAtYLabel = new QLabel(tr("y:"));

    QDoubleSpinBox *lookAtYDoubleSpinBox = new QDoubleSpinBox();
    lookAtYDoubleSpinBox->setRange(-50.0, 50.0);
    lookAtYDoubleSpinBox->setSingleStep(1.0);
    lookAtYDoubleSpinBox->setValue(lookAtY);
    connect(lookAtYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeLookAtY(double)));


    QLabel *lookAtZLabel = new QLabel(tr("z:"));

    QDoubleSpinBox *lookAtZDoubleSpinBox = new QDoubleSpinBox();
    lookAtZDoubleSpinBox->setRange(-50.0, 50.0);
    lookAtZDoubleSpinBox->setSingleStep(1.0);
    lookAtZDoubleSpinBox->setValue(lookAtZ);
    connect(lookAtZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeLookAtZ(double)));


    QHBoxLayout *lookAtLayout = new QHBoxLayout;
    lookAtLayout->addWidget(lookAtXLabel);
    lookAtLayout->addWidget(lookAtXDoubleSpinBox);
    lookAtLayout->addStretch(1);
    lookAtLayout->addWidget(lookAtYLabel);
    lookAtLayout->addWidget(lookAtYDoubleSpinBox);
    lookAtLayout->addStretch(1);
    lookAtLayout->addWidget(lookAtZLabel);
    lookAtLayout->addWidget(lookAtZDoubleSpinBox);
    lookAtLayout->addStretch(1);

    QGroupBox *lookAtGroupBox = new QGroupBox(QString::fromUtf8("LookAt (ponto)"));
    lookAtGroupBox->setLayout(lookAtLayout);




    QLabel *upXLabel = new QLabel(tr("x:"));

    QDoubleSpinBox *upXDoubleSpinBox = new QDoubleSpinBox();
    upXDoubleSpinBox->setRange(-50.0, 50.0);
    upXDoubleSpinBox->setSingleStep(1.0);
    upXDoubleSpinBox->setValue(upX);
    connect(upXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeUpX(double)));


    QLabel *upYLabel = new QLabel(tr("y:"));

    QDoubleSpinBox *upYDoubleSpinBox = new QDoubleSpinBox();
    upYDoubleSpinBox->setRange(-50.0, 50.0);
    upYDoubleSpinBox->setSingleStep(1.0);
    upYDoubleSpinBox->setValue(upY);
    connect(upYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeUpY(double)));


    QLabel *upZLabel = new QLabel(tr("z:"));

    QDoubleSpinBox *upZDoubleSpinBox = new QDoubleSpinBox();
    upZDoubleSpinBox->setRange(-50.0, 50.0);
    upZDoubleSpinBox->setSingleStep(1.0);
    upZDoubleSpinBox->setValue(upZ);
    connect(upZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeUpZ(double)));


    QHBoxLayout *upLayout = new QHBoxLayout;
    upLayout->addWidget(upXLabel);
    upLayout->addWidget(upXDoubleSpinBox);
    upLayout->addStretch(1);
    upLayout->addWidget(upYLabel);
    upLayout->addWidget(upYDoubleSpinBox);
    upLayout->addStretch(1);
    upLayout->addWidget(upZLabel);
    upLayout->addWidget(upZDoubleSpinBox);
    upLayout->addStretch(1);

    QGroupBox *upGroupBox = new QGroupBox(QString::fromUtf8("Up (vetor)"));
    upGroupBox->setLayout(upLayout);


    QDoubleSpinBox *fDoubleSpinBox = new QDoubleSpinBox();
    fDoubleSpinBox->setRange(0.0, 50.0);
    fDoubleSpinBox->setSingleStep(1.0);
    fDoubleSpinBox->setValue(f);
    connect(fDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeF(double)));

    QHBoxLayout *fLayout = new QHBoxLayout;
    fLayout->addWidget(fDoubleSpinBox);
    fLayout->addStretch(1);

    QGroupBox *fGroupBox = new QGroupBox(QString::fromUtf8("Distância focal"));
    fGroupBox->setLayout(fLayout);


    QSpinBox *mSpinBox = new QSpinBox();
    mSpinBox->setRange(0.0, MAX);
    mSpinBox->setSingleStep(10.0);
    mSpinBox->setValue(m);
    connect(mSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeM(int)));

    QSpinBox *nSpinBox = new QSpinBox();
    nSpinBox->setRange(0.0, MAX);
    nSpinBox->setSingleStep(10.0);
    nSpinBox->setValue(n);
    connect(nSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeN(int)));


    QLabel *mLabel = new QLabel(tr("Largura:"));
    QLabel *nLabel = new QLabel(tr("Altura:"));

    QHBoxLayout *mnLayout = new QHBoxLayout;
    mnLayout->addWidget(mLabel);
    mnLayout->addWidget(mSpinBox);
    mnLayout->addWidget(nLabel);
    mnLayout->addWidget(nSpinBox);
    mnLayout->addStretch(1);

    QGroupBox *mnGroupBox = new QGroupBox(QString::fromUtf8("Grade"));
    mnGroupBox->setLayout(mnLayout);


    QPushButton *setCameraPushButton = new QPushButton("Atualizar", this);
    connect(setCameraPushButton, SIGNAL(clicked()), this, SLOT(updateCamera()));


    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(eyeGroupBox);
    layout->addWidget(lookAtGroupBox);
    layout->addWidget(upGroupBox);
    layout->addWidget(mnGroupBox);
    layout->addWidget(fGroupBox);
    layout->addWidget(setCameraPushButton);
	layout->addStretch(1);

    setLayout(layout);
}

CameraPanel::~CameraPanel(){
}

void CameraPanel::updateCamera(){
    camera->setEye(Vector3D(eyeX, eyeY, eyeZ));
    camera->setLookAt(Vector3D(lookAtX, lookAtY, lookAtZ));
    camera->setUp(Vector3D(upX, upY, upZ));
    camera->setM(m);
    camera->setN(n);
    camera->setFocalDistance(f);

    emit update();
}

void CameraPanel::changeEyeX(double _x){
    eyeX = _x;
}

void CameraPanel::changeEyeY(double _y){
    eyeY = _y;
}

void CameraPanel::changeEyeZ(double _z){
    eyeZ = _z;
}

void CameraPanel::changeLookAtX(double _x){
    lookAtX = _x;
}

void CameraPanel::changeLookAtY(double _y){
    lookAtY = _y;
}

void CameraPanel::changeLookAtZ(double _z){
    lookAtZ = _z;
}

void CameraPanel::changeUpX(double _x){
    upX = _x;
}

void CameraPanel::changeUpY(double _y){
    upY = _y;
}

void CameraPanel::changeUpZ(double _z){
    upZ = _z;
}

void CameraPanel::changeM(int _m){
    m = _m;
}

void CameraPanel::changeN(int _n){
    n = _n;
}

void CameraPanel::changeF(double _f){
    f = _f;
}

void CameraPanel::setCamera(Camera *_c){
    camera = _c;

    camera->setEye(Vector3D(eyeX, eyeY, eyeZ));
    camera->setLookAt(Vector3D(lookAtX, lookAtY, lookAtZ));
    camera->setUp(Vector3D(upX, upY, upZ));
    camera->setM(m);
    camera->setN(n);
    camera->setFocalDistance(f);
};
